<?php
require_once('login.php');

class User extends MY_Controller {

	private $salt;

	function User() {
        parent::MY_Controller();
		
        $this->load->plugin('jpgraph');
        
        $this->ci =& get_instance();
		$this->salt = $this->ci->config->item('encryption_key');
		
		$this->ci =& get_instance();
		$this->points_perfect    = $this->ci->config->item('points_perfect');
		$this->points_difference = $this->ci->config->item('points_difference');
		$this->points_tendency   = $this->ci->config->item('points_tendency');
		
	}

	function index () {
		$data = $this->getData("user");

		$sql = "SELECT id, username, name, email, settings FROM ".$this->tblu." WHERE id = ?";
		$query = $this->db->query($sql, array($this->fromUser));
		$res = $query->result();
		
		$d['userselection'] = $this->load->view('selection-user', $this->getUser('user/'), true);
		
		$u = json_decode($res[0]->settings);
		$u->id = $res[0]->id;
		$u->username = $res[0]->username;
		$u->name  = $res[0]->name;
		$u->email = $res[0]->email;
		$d['userdata'] = $u;
		
		// Gesamtpunkte pro Spieltag
		$pt = $this->points_tendency;
		$pd = $this->points_difference;
		$pp = $this->points_perfect;
		
		$sql = "SELECT"
			." matchgroup,"
			." sum(tendency*$pt + difference*$pd + perfect*$pp) as points"
			." FROM ".$this->tblp
			." WHERE user_id = ?"
			." GROUP BY matchgroup"
			." ORDER BY LENGTH(matchgroup) ASC, matchgroup ASC";
	
		$query = $this->db->query($sql, array($this->fromUser));
		$matchdaypoints = array ();
		$matchdaypoints_label = array ();
		foreach ($query->result() as $res) {
	    	$matchdaypoints[] = $res->points;
	    	$matchdaypoints_label[] = $res->matchgroup.".";
	    }
		$chartname = "matchdaypoints_".$this->fromUser."_".sizeof($matchdaypoints); 
		$config['xTitle'] = "Spieltag";
		$config['yTitle'] = "Punkte";
		
		$d['chart_matchdaypoints'] = null;
		if (sizeof($matchdaypoints) > 0) {
			$d['chart_matchdaypoints'] = barchart($chartname, $matchdaypoints, $matchdaypoints_label, $config);
		}
			
		
		
		
		$d['isOwner'] = $this->isOwner;
		$data["content"] = $this->load->view('user', $d, true);
		$this->load->view('layout', $data);
	}
	
	function save() {
        $this->load->library('form_validation');
        		
		$url  = $this->input->post('url');
		$m_t  = $this->input->post('messenger_type');
		$m_v  = $this->input->post('messenger_value');

		$this->session->set_flashdata("status", lang('userdata_changed'));

		// update db
		$settings = generateUserSettings($url, $m_t, $m_v);
		$sql = "UPDATE ".$this->tblu." SET settings = ? WHERE id = ? LIMIT 1";
		$query = $this->db->query($sql, array(json_encode($settings), $this->user_id));
		
		// update session
		Login::fillSession($this, $this->user_id);

		// update password
		$pw   = $this->input->post('password');
		$pw_r = $this->input->post('retype');

		// Set the validation rules.
		$this->form_validation->set_rules('password', 'Passwort', 'min_length[5]|matches[retype]');
		$this->form_validation->set_rules('retype', 'Password Wiederholung', '');

		$this->form_validation->set_error_delimiters('<span>','</span>'); // Set the the error tags.

		if($this->form_validation->run()) {

			if (strlen($pw) >0){

				$hash = sha1(microtime());
				$password_enc = sha1($this->salt.$hash.$pw);
				$values = array($hash, $password_enc, $this->user_id);
				
				$sql = "UPDATE ".$this->tblu." SET hash = ?, password = ? WHERE id = ? LIMIT 1";
				$query = $this->db->query($sql, $values);

				$this->session->set_flashdata("status", lang('password_changed'));
				// TODO:: password change
			}
			redirect('user');
		}
		else {
			// Validation Failed
			$this->session->set_flashdata("status", $this->form_validation->error_string());
			redirect('user#form');
		}

		redirect('user');
	}

	function changefromuser() {
		$user_id  = $this->input->post('user');
		$redirect = $this->input->post('redirect');
		$this->changeUser($user_id, $redirect);
	}
	
	
	function showuser($user_id, $redirect) {
		$this->changeUser($user_id, $redirect);
	}

	private function changeUser($user_id, $redirect) {

		if (strlen($user_id) > 0)
			$this->session->set_userdata("fromUser", $user_id);
		else
			$this->session->set_userdata("fromUser", $this->user_id);

		redirect($redirect);
	}


	
}


?>